<template>
    <el-dialog :visible.sync="updateLabelDialogVisible" :title="$t('photoinfo.updateLabelName')"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
                 label-width="120px">
            <el-form-item prop="photoLabelList" :label="$t('photoinfo.labelName')">
                <el-select
                        v-model="dataForm.photoLabelList"
                        multiple
                        filterable
                        remote
                        placeholder="请输入关键词"
                        :remote-method="getUpdateLabelphotoLabel"
                        :loading="updateLabelLoading">
                    <el-option
                            v-for="item in this.updateLabelphotoLabel"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template slot="footer">
            <el-button @click="updateLabelDialogVisible = false">{{ $t('cancel') }}</el-button>
            <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
        </template>
    </el-dialog>
</template>

<script>
    import debounce from 'lodash/debounce'

    export default {
        data() {
            return {
                updateLabelDialogVisible: false,
                dataForm: {
                    id:'',
                    photoLabelList: [],
                },
                updateLabelphotoLabel: [],
                updateLabelLoading: false,
            }
        },
        computed: {
            dataRule() {
                return {
                    photoLabelList: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            dataFormSubmitHandle: debounce(function () {
                this.$refs['dataForm'].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    this.$http['put']('/photo/info', this.dataForm).then(({ data: res }) => {
                        if (res.code !== 0) {
                            return this.$message.error(res.msg)
                        }
                        this.$message({
                            message: this.$t('prompt.success'),
                            type: 'success',
                            duration: 500,
                            onClose: () => {
                                this.updateLabelDialogVisible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    }).catch(() => {})
                })
            }, 1000, {'leading': true, 'trailing': false}),
            getUpdateLabelphotoLabel(query) {
                console.log('1')
                this.updateLabelLoading = true
                this.$http.get('/photo/label/select-options?query=' + query).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.updateLabelLoading = false
                    this.updateLabelphotoLabel = res.data.labelSelectOptions
                }).catch(() => {
                })
            },
            //根据照片信息回显标签
            showPhtotLabelByInfoId(infoId) {
                this.updateLabelDialogVisible = true
                console.log(1111)
                this.getUpdateLabelphotoLabel('')
                this.dataForm.photoLabelList = []
                this.dataForm.id=infoId

                this.updateLabelLoading = true
                this.$http.get('/photo/info/get-label?infoId=' + infoId).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.updateLabelLoading = false
                    for (let item of res.data.labelSelectOptions) {
                        this.dataForm.photoLabelList.push(item.id)
                    }
                }).catch(() => {
                })
            }
        }
    }
</script>